package _01_动态数组;

import java.util.*;

public class _384_打乱数组 {

    public static void main(String[] args) {

        int[] ins = new int[]{1, 2, 3, 4, 5, 6};


        _384_打乱数组 v = new _384_打乱数组(ins);

        System.out.println(Arrays.toString(v.shuffle()));
        System.out.println(Arrays.toString(v.shuffle()));

        System.out.println();

    }

    private final int[] nums;

    private final Random random = new Random();

    public _384_打乱数组(int[] nums) {
        this.nums = nums;
    }

    public int[] reset() {
        return nums;
    }

    public int[] shuffle() {
        int[] newAry = new int[nums.length];
        System.arraycopy(nums, 0, newAry, 0, nums.length);
        // 获取随机打乱数据
        dfs(nums.length - 1, newAry);
        return newAry;
    }


    private void dfs(int i, int[] nums) {
        if (i < 0) {
            return;
        }

        int randomNum = random.nextInt(i + 1);

        // 和当前位置交换
        int cur = nums[i];
        nums[i] = nums[randomNum];
        nums[randomNum] = cur;

        dfs(i - 1, nums);
    }

}
